package javabase.schoolwork.day5_28;

import java.util.Arrays;

/**
 * @author fance
 * @date 2018/5/29 9:56
 */
public class Array {

    protected int[] elementData;
    private int size;
    private final int INIT_SIZE = 16;
    protected int foot;

    public Array() {
        size = INIT_SIZE;
        this.elementData = new int[INIT_SIZE];

    }

    public Array(int init_size) {
        size = init_size;
        this.elementData = new int[init_size];

    }

    public boolean add(int val) {
        if (foot == size) {
            grow();
        }
        elementData[foot++] = val;
        return true;
    }
    private void grow() {
        size *= 2;
        elementData = Arrays.copyOf(elementData, size);
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < foot; i++) {
            stringBuilder.append(elementData[i]).append(",");
        }
        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Array array = new Array();
        for (int i = 0; i < 20; i++) {
            array.add(i);
        }
        System.out.println(array);

    }
}
